package visiopuce.ui.view;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.window.ToolTip;
import org.eclipse.nebula.widgets.formattedtext.FormattedText;
import org.eclipse.nebula.widgets.formattedtext.NumberFormatter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wb.swt.ResourceManager;
import org.eclipse.wb.swt.SWTResourceManager;

import visiopuce.ApplicationContexte;
import visiopuce.graph.GraphCytog;
import visiopuce.objets.Alteration;
import visiopuce.objets.Annotation;
import visiopuce.objets.GeneNcbi;
import visiopuce.objets.Puce;
import visiopuce.objets.TypeCausal;
import visiopuce.objets.TypePuce;
import visiopuce.objets.Utilisateur;
import visiopuce.objets.VerifBiomol;
import visiopuce.objets.VerifFish;
import visiopuce.service.AlterationServiceImpl;
import visiopuce.service.DroitServiceImpl;
import visiopuce.service.PuceServiceImpl;
import visiopuce.service.ReferentielServiceImpl;
import visiopuce.service.ResultatServiceImpl;
import visiopuce.service.UtilisateurServiceImpl;
import visiopuce.service.document.AlterationDoc;
import visiopuce.ui.listener.ExportXLSListener;
import visiopuce.ui.providers.GeneTableLabelProvider;
import visiopuce.ui.providers.TypeCausalLabelProvider;
import visiopuce.ui.providers.UtilisateurLabelProvider;

public class CNVTabItem implements IDossierTabItem<Alteration> {

	private Alteration alteration;
	private TabItem cnvTabItem;
	private boolean isModifiable;
	private FormattedText tConfidence;
	private FormattedText tNbGenes;
	private FormattedText tLog2R;
	private FormattedText tNbCopie;
	private FormattedText tNbMq;
	private FormattedText tTaille;
	private Text tSondeFin;
	private Text tSondeDebut;
	private FormattedText tFin;
	private FormattedText tDebut;
	private Combo comboChr;
	private Text tGenesInteret;
	private Text tResultatVerif;
	private Composite composite;
	private Group grpGraphique;
	private Link linkNCBI;
	private Link linkEnsembl;
	private Link linkDGV;
	private Link linkDecipher;
	private Link linkUCSC;
	private Link linkPubMedBande;
	private Text tCommentairePA;
	private Text tCommentaireS;
	private Group grpGnesPrsentsDans;
	private ComboViewer comboPreAna;
	private ComboViewer comboSign;
	private Combo comboNoteS;
	private Combo comboNotePA;
	private ComboViewer comboQualifS;
	private ComboViewer comboQualifPA;
	private Button btnCheckVerifBM;
	private Button btnCheckVerifFISH;
	private TableViewer tableGenes;
	private boolean colonneExiste = false;
	private ModifyListener modifyListener;
	private SelectionAdapter selectionListener;
	private SelectionAdapter linkSelectionListener;
	private int selectedColumn = -1;
	private GraphCytog graph;
	private Annotation annotationPA;
	private Annotation annotationS;
	private Combo comboAnnotationFinale;
	private Button btManuellement;
	private Button btnCheckPrFormule;
	private Button export;
	private ScrolledComposite sc1;
	private ToolItem itemPrint;

	public CNVTabItem(TabFolder parent, int style, boolean modifiable) {
		cnvTabItem = new TabItem(parent, style);
		modifyListener = new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if (((DossierEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor()) != null) {
					((DossierEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor()).setDirty(true);
				}
			}
		};
		selectionListener = new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				((DossierEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor()).setDirty(true);
			}
		};
		linkSelectionListener = new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(new URL(e.text));
				} catch (PartInitException ex) {
					// TODO Auto-generated catch block
					ex.printStackTrace();
				} catch (MalformedURLException ex) {
					// TODO Auto-generated catch block
					ex.printStackTrace();
				}
			}
		};
		this.isModifiable = modifiable;
		createContent();
	}

	private void addDirtyListener() {

		tConfidence.getControl().addModifyListener(modifyListener);
		tLog2R.getControl().addModifyListener(modifyListener);
		tNbCopie.getControl().addModifyListener(modifyListener);
		tNbMq.getControl().addModifyListener(modifyListener);
		tSondeFin.addModifyListener(modifyListener);
		tSondeDebut.addModifyListener(modifyListener);
		tFin.getControl().addModifyListener(modifyListener);
		tDebut.getControl().addModifyListener(modifyListener);
		comboChr.addModifyListener(modifyListener);
		tGenesInteret.addModifyListener(modifyListener);
		tCommentairePA.addModifyListener(modifyListener);
		tCommentaireS.addModifyListener(modifyListener);
		comboPreAna.getCombo().addModifyListener(modifyListener);
		comboSign.getCombo().addModifyListener(modifyListener);
		comboNoteS.addModifyListener(modifyListener);
		comboNotePA.addModifyListener(modifyListener);
		comboQualifS.getCombo().addModifyListener(modifyListener);
		comboQualifPA.getCombo().addModifyListener(modifyListener);
		btnCheckVerifBM.addSelectionListener(selectionListener);
		btnCheckVerifFISH.addSelectionListener(selectionListener);
		btnCheckPrFormule.addSelectionListener(selectionListener);
		comboAnnotationFinale.addModifyListener(modifyListener);
	}

	public void createContent() {

		ArrayList<Utilisateur> listSignataires = UtilisateurServiceImpl.getInstance().getBiologisteByTypeAnalyse(3);
		ArrayList<Utilisateur> listAnnots = UtilisateurServiceImpl.getInstance().getAnnotateurs();
		ArrayList<TypeCausal> listTypeCausals = ReferentielServiceImpl.getInstance().getTypeCausals();

		sc1 = new ScrolledComposite(cnvTabItem.getParent(), SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		sc1.getVerticalBar().setIncrement(10);
		sc1.getHorizontalBar().setIncrement(10);
		sc1.setAlwaysShowScrollBars(true);
		composite = new Composite(sc1, SWT.NONE);
		sc1.setContent(composite);
		sc1.getVerticalBar().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				if (graph != null) {
					graph.repaint();
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}

		});
		cnvTabItem.setControl(sc1);

		composite.setLayout(new GridLayout(4, false));

		if (DroitServiceImpl.getInstance().peutLireByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "alteration")) {
			ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.RIGHT);
			toolBar.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 4, 1));
			itemPrint = new ToolItem(toolBar, SWT.PUSH);
			itemPrint.setImage(ResourceManager.getPluginImage("visiopuce", "icons/imprimer.png"));
			itemPrint.setToolTipText("Imprimer");
			itemPrint.setEnabled(true);
			itemPrint.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					AlterationDoc altDoc = new AlterationDoc();
					String out = altDoc.createDocument(alteration, CNVTabItem.this);
					File fichierCNV = new File(ApplicationContexte.REPERTOIRE_DOCUMENT + out);
					Desktop desktop = Desktop.getDesktop();
					try {
						if (fichierCNV != null) {
							desktop.open(fichierCNV);
						}
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});

		}

		Label lblChromosome = new Label(composite, SWT.NONE);
		lblChromosome.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblChromosome.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblChromosome.setText("Chromosome* :");

		comboChr = new Combo(composite, SWT.READ_ONLY);
		comboChr.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		comboChr.setItems(ApplicationContexte.chr);

		btManuellement = new Button(composite, SWT.CHECK);
		btManuellement.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 2, 1));
		btManuellement.setText("CNV entré manuellement");
		btManuellement.setEnabled(false);

		Label lblDbut = new Label(composite, SWT.NONE);
		lblDbut.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblDbut.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblDbut.setText("D\u00E9but* :");

		tDebut = new FormattedText(composite, SWT.BORDER);
		tDebut.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tDebut.setFormatter(new NumberFormatter(ApplicationContexte.nfEntier, Locale.FRENCH));

		Label lblFin = new Label(composite, SWT.NONE);
		lblFin.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblFin.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblFin.setText("Fin* :");

		tFin = new FormattedText(composite, SWT.BORDER);
		tFin.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tFin.setFormatter(new NumberFormatter(ApplicationContexte.nfEntier, Locale.FRENCH));

		Label lblSondeDebut = new Label(composite, SWT.NONE);
		lblSondeDebut.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblSondeDebut.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblSondeDebut.setText("1er sonde d\u00E9viante :");

		tSondeDebut = new Text(composite, SWT.BORDER);
		tSondeDebut.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label lblSondeFin = new Label(composite, SWT.NONE);
		lblSondeFin.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblSondeFin.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblSondeFin.setText("Derni\u00E8re sonde d\u00E9viante : ");

		tSondeFin = new Text(composite, SWT.BORDER);
		tSondeFin.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label lblTaille = new Label(composite, SWT.NONE);
		lblTaille.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblTaille.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblTaille.setText("Taille (kb) : ");

		tTaille = new FormattedText(composite, SWT.BORDER);
		tTaille.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tTaille.setFormatter(new NumberFormatter(ApplicationContexte.nf2AV, Locale.US));

		Label lblNbMq = new Label(composite, SWT.NONE);
		lblNbMq.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNbMq.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblNbMq.setText("Nombre de marqueurs d\u00E9viants* : ");

		tNbMq = new FormattedText(composite, SWT.BORDER);
		tNbMq.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tNbMq.setFormatter(new NumberFormatter(ApplicationContexte.nfEntier, Locale.FRENCH));

		Label lblNombreDeCopies = new Label(composite, SWT.NONE);
		lblNombreDeCopies.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblNombreDeCopies.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNombreDeCopies.setText("Nombre de copies** : ");

		tNbCopie = new FormattedText(composite, SWT.BORDER);
		tNbCopie.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tNbCopie.setFormatter(new NumberFormatter(ApplicationContexte.nfEntier, Locale.FRENCH));

		Label lblLogratio = new Label(composite, SWT.NONE);
		lblLogratio.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblLogratio.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblLogratio.setText("Log2Ratio** : ");

		tLog2R = new FormattedText(composite, SWT.BORDER);
		tLog2R.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tLog2R.setFormatter(new NumberFormatter(ApplicationContexte.nf4AVneg, Locale.US));

		Label lblNombreDeGnes = new Label(composite, SWT.NONE);
		lblNombreDeGnes.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNombreDeGnes.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblNombreDeGnes.setText("Nombre de g\u00E8nes : ");

		tNbGenes = new FormattedText(composite, SWT.BORDER);
		tNbGenes.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tNbGenes.setFormatter(new NumberFormatter(ApplicationContexte.nfEntier, Locale.FRENCH));

		Label lblConfidence = new Label(composite, SWT.NONE);
		lblConfidence.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblConfidence.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblConfidence.setText("Confidence : ");

		tConfidence = new FormattedText(composite, SWT.BORDER);
		tConfidence.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tConfidence.setFormatter(new NumberFormatter(ApplicationContexte.nf2AV, Locale.US));

		grpGraphique = new Group(composite, SWT.NONE);
		grpGraphique.setText("Graphique");
		grpGraphique.setLayout(new GridLayout(1, false));
		GridData gd_grpGraphique = new GridData(SWT.FILL, SWT.CENTER, true, true, 4, 4);
		grpGraphique.setLayoutData(gd_grpGraphique);

		Group grpLiensVersDautres = new Group(composite, SWT.NONE);
		grpLiensVersDautres.setLayout(new GridLayout(6, false));
		grpLiensVersDautres.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 4, 1));
		grpLiensVersDautres.setText("Liens vers d'autres bases :");

		linkNCBI = new Link(grpLiensVersDautres, SWT.NONE);
		linkNCBI.setText("<a>NCBI Map Viewer</a>");
		linkNCBI.addSelectionListener(linkSelectionListener);

		linkEnsembl = new Link(grpLiensVersDautres, SWT.NONE);
		linkEnsembl.setText("<a>ENSEMBL</a>");
		linkEnsembl.addSelectionListener(linkSelectionListener);

		linkDGV = new Link(grpLiensVersDautres, SWT.NONE);
		linkDGV.setText("<a>DGV</a>");
		linkDGV.addSelectionListener(linkSelectionListener);

		linkDecipher = new Link(grpLiensVersDautres, SWT.NONE);
		linkDecipher.setText("<a>Decipher</a>");
		linkDecipher.addSelectionListener(linkSelectionListener);

		linkUCSC = new Link(grpLiensVersDautres, SWT.NONE);
		linkUCSC.setText("<a>UCSC</a>");
		linkUCSC.addSelectionListener(linkSelectionListener);

		// linkPubMedBande = new Link(grpLiensVersDautres, SWT.NONE);
		// linkPubMedBande.setText("<a>PubMed par bande chromosomique</a>");

		grpGnesPrsentsDans = new Group(composite, SWT.NONE);
		GridData gd_grpGnesPrsentsDans = new GridData(SWT.LEFT, SWT.FILL, true, false, 4, 1);
		// gd_grpGnesPrsentsDans.widthHint = 559;
		grpGnesPrsentsDans.setLayoutData(gd_grpGnesPrsentsDans);
		grpGnesPrsentsDans.setLayout(new GridLayout(1, false));
		grpGnesPrsentsDans.setText("G\u00E8nes chevauchants le CNV");

		tableGenes = new TableViewer(grpGnesPrsentsDans, SWT.BORDER | SWT.FULL_SELECTION);
		tableGenes.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		tableGenes.getTable().setHeaderVisible(true);
		tableGenes.getTable().setLinesVisible(true);
		tableGenes.setContentProvider(ArrayContentProvider.getInstance());
		tableGenes.getTable().addListener(SWT.MouseDown, new Listener() {
			public void handleEvent(Event event) {
				Point pt = new Point(event.x, event.y);
				TableItem item = tableGenes.getTable().getItem(pt);
				if (item != null) {
					for (int col = 0; col < tableGenes.getTable().getColumnCount(); col++) {
						Rectangle rect = item.getBounds(col);
						if (rect.contains(pt)) {
							selectedColumn = col;
						}
					}
				}
			}
		});

		tableGenes.addDoubleClickListener(new IDoubleClickListener() {

			@Override
			public void doubleClick(DoubleClickEvent event) {
				Object selectedItem = ((StructuredSelection) event.getSelection()).getFirstElement();
				String url = "";
				if (selectedItem instanceof GeneNcbi) {

					GeneNcbi gene = (GeneNcbi) selectedItem;
					switch (selectedColumn) {
					case 0:
						url = "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=gene&amp;cmd=retrieve&amp;dopt=full_report&amp;list_uids=" + gene.getId().getIdNcbi();
						break;
					case 1:
						url = null;
						break;
					case 2: {
						url = null;
						break;
					}
					case 3:
						url = null;
						break;
					case 4: {
						if (gene.getDisorderMimnb() != null) {
							String[] numeros = gene.getDisorderMimnb().split("\\|");
							url = "";
							for (String numero : numeros) {
								url += "http://omim.org/entry/" + numero + "|";
							}

						} else {
							url = "-";
						}
						break;
					}

					case 5: {
						url = null;
						break;
					}
					case 6: {
						url = null;
						break;
					}
					case 7: {
						if (gene.getMimnb() != null) {
							url = "http://omim.org/entry/" + gene.getMimnb();
						} else {
							url = null;
						}
						break;
					}
					case 8:
						url = "http://www.genecards.org/cgi-bin/carddisp.pl?gene=" + gene.getSymbol();
						break;

					case 9:
						url = "http://www.ensembl.org/Human/Search/Results?q=" + gene.getSymbol() + ";y=0;site=ensembl;x=0;facet_species=Human;page=1;facet_feature_type=Gene";
						break;
					case 10:
						url = "http://www.ncbi.nlm.nih.gov/pubmed?term=" + gene.getSymbol();
						break;

					case 11:
						url = "http://www.ncbi.nlm.nih.gov/gquery/?term=" + gene.getSymbol();
						break;

					case 12:
						url = "http://projects.tcag.ca/variation/search.asp?source=hg19&table=Locus&filtre=&show=table&exactMatch=no&keyword=" + gene.getSymbol();
						break;

					case 13:
						url = "http://genome.ucsc.edu/cgi-bin/hgGene?hgg_type=knownGene&db=hg19&hgg_gene=" + gene.getSymbol();
						break;
					}
					try {
						if (url != null && !url.isEmpty() && url.contains("http:")) {
							String[] urls = url.split("\\|");
							for (String uri : urls) {
								PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(new URL(uri));
							}
						}
					} catch (PartInitException | MalformedURLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				// process selectedItem which is our row item as provided
				// by our IStructuredContentProvider, selectedColumn
				// is the index of column (or -1 if there was problem)
			}
		});

		export = new Button(grpGnesPrsentsDans, SWT.BORDER | SWT.FULL_SELECTION);
		export.setImage(ResourceManager.getPluginImage("visiopuce", "icons/Excel-icon.png"));

		// Préanalyse
		Group grpInterprtationPA = new Group(composite, SWT.NONE);
		grpInterprtationPA.setLayout(new GridLayout(4, false));
		GridData gd_grpInterprtationPA = new GridData(SWT.LEFT, SWT.CENTER, false, false, 4, 1);
		gd_grpInterprtationPA.widthHint = 560;
		grpInterprtationPA.setLayoutData(gd_grpInterprtationPA);
		grpInterprtationPA.setText("Pr\u00E9-analyse");

		Label lblAnalysParPA = new Label(grpInterprtationPA, SWT.NONE);
		lblAnalysParPA.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblAnalysParPA.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblAnalysParPA.setText("Analys\u00E9 par :");

		comboPreAna = new ComboViewer(grpInterprtationPA, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboPreAna.setContentProvider(ArrayContentProvider.getInstance());
		comboPreAna.setLabelProvider(new UtilisateurLabelProvider());
		comboPreAna.setInput(listAnnots);

		new Label(grpInterprtationPA, SWT.NONE);
		new Label(grpInterprtationPA, SWT.NONE);

		Label lblNewLabelPA = new Label(grpInterprtationPA, SWT.NONE);
		lblNewLabelPA.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblNewLabelPA.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabelPA.setText("Qualification :");

		comboQualifPA = new ComboViewer(grpInterprtationPA, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboQualifPA.setContentProvider(ArrayContentProvider.getInstance());
		comboQualifPA.setLabelProvider(new TypeCausalLabelProvider());
		comboQualifPA.setInput(listTypeCausals);
		comboQualifPA.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label lblNotePA = new Label(grpInterprtationPA, SWT.NONE);
		lblNotePA.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNotePA.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblNotePA.setText("Note :");

		comboNotePA = new Combo(grpInterprtationPA, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboNotePA.setItems(ApplicationContexte.notes);
		comboNotePA.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(grpInterprtationPA, SWT.NONE);

		tCommentairePA = new Text(grpInterprtationPA, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		GridData gd_textCommentairePA = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 2);
		gd_textCommentairePA.heightHint = 150;
		tCommentairePA.setLayoutData(gd_textCommentairePA);

		Label lblCommentairePA = new Label(grpInterprtationPA, SWT.NONE);
		lblCommentairePA.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblCommentairePA.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblCommentairePA.setText("Commentaire :");

		Group grpInterprtation = new Group(composite, SWT.NONE);
		grpInterprtation.setLayout(new GridLayout(4, false));
		GridData gd_grpInterprtation = new GridData(SWT.LEFT, SWT.CENTER, false, false, 4, 1);
		gd_grpInterprtation.widthHint = 560;
		grpInterprtation.setLayoutData(gd_grpInterprtation);
		grpInterprtation.setText("Relecture");

		Label lblAnalysPar = new Label(grpInterprtation, SWT.NONE);
		lblAnalysPar.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblAnalysPar.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblAnalysPar.setText("Analys\u00E9 par :");

		comboSign = new ComboViewer(grpInterprtation, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboSign.setContentProvider(ArrayContentProvider.getInstance());
		comboSign.setLabelProvider(new UtilisateurLabelProvider());
		comboSign.setInput(listSignataires);
		new Label(grpInterprtation, SWT.NONE);
		new Label(grpInterprtation, SWT.NONE);

		Label lblNewLabel = new Label(grpInterprtation, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("Qualification :");

		comboQualifS = new ComboViewer(grpInterprtation, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboQualifS.setContentProvider(ArrayContentProvider.getInstance());
		comboQualifS.setLabelProvider(new TypeCausalLabelProvider());
		comboQualifS.setInput(listTypeCausals);
		comboQualifS.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label lblNote = new Label(grpInterprtation, SWT.NONE);
		lblNote.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNote.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblNote.setText("Note :");

		comboNoteS = new Combo(grpInterprtation, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboNoteS.setItems(ApplicationContexte.notes);
		comboNoteS.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(grpInterprtation, SWT.NONE);

		tCommentaireS = new Text(grpInterprtation, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		GridData gd_textCommentaire = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 2);
		gd_textCommentaire.heightHint = 150;
		tCommentaireS.setLayoutData(gd_textCommentaire);

		Label lblCommentaire = new Label(grpInterprtation, SWT.NONE);
		lblCommentaire.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblCommentaire.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblCommentaire.setText("Commentaire :");

		Group grpVrification = new Group(composite, SWT.NONE);
		grpVrification.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.NORMAL));
		grpVrification.setText("V\u00E9rification");
		grpVrification.setLayout(new GridLayout(2, false));
		GridData gd_grpVrification = new GridData(SWT.LEFT, SWT.CENTER, false, false, 4, 1);
		gd_grpVrification.widthHint = 565;
		grpVrification.setLayoutData(gd_grpVrification);

		btnCheckVerifBM = new Button(grpVrification, SWT.CHECK);
		btnCheckVerifBM.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		btnCheckVerifBM.setText("CNV \u00E0 v\u00E9rifier par qPCR");

		btnCheckVerifFISH = new Button(grpVrification, SWT.CHECK);
		btnCheckVerifFISH.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		btnCheckVerifFISH.setText("CNV \u00E0 v\u00E9rifier par FISH");

		btnCheckPrFormule = new Button(grpVrification, SWT.CHECK);
		btnCheckPrFormule.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		btnCheckPrFormule.setText("CNV \u00E0 ajouter à la formule sans vérification");

		new Label(grpVrification, SWT.NONE);

		Label lblGnesDintret = new Label(grpVrification, SWT.NONE);
		lblGnesDintret.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblGnesDintret.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblGnesDintret.setText("G\u00E8nes d'int\u00E9ret :");

		tGenesInteret = new Text(grpVrification, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		tGenesInteret.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label lblRsultatDeLa = new Label(grpVrification, SWT.NONE);
		lblRsultatDeLa.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblRsultatDeLa.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblRsultatDeLa.setText("R\u00E9sultat de la v\u00E9rification :");

		tResultatVerif = new Text(grpVrification, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		tResultatVerif.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label lblAnnotationFinale = new Label(grpVrification, SWT.NONE);
		lblAnnotationFinale.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblAnnotationFinale.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblAnnotationFinale.setText("Qualification finale du CNV :");

		comboAnnotationFinale = new Combo(grpVrification, SWT.READ_ONLY);
		comboAnnotationFinale.setItems(ApplicationContexte.annotations);
		comboAnnotationFinale.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(grpInterprtation, SWT.NONE);

		setModifiable(isModifiable);
		cnvTabItem.getParent().setSelection(cnvTabItem);
		composite.pack();
		sc1.redraw();
	}

	public TabItem getTabItem() {
		return cnvTabItem;
	}

	public boolean isModifiable() {
		return isModifiable;
	}

	public Alteration recupererObjet() {
		if (verifierChamp()) {
			SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
			Date datedujour = new Date();
			if (tConfidence.getValue() != null) {
				alteration.setConfidence((Double) tConfidence.getValue());
			}
			if (tLog2R.getValue() != null) {
				alteration.setLog2R((Double) tLog2R.getValue());
			}
			if (tNbMq.getValue() != null) {
				int nbmq = 0;
				if (tNbMq.getValue() instanceof Integer) {
					nbmq = ((Integer) tNbMq.getValue()).intValue();
				}
				alteration.setNbMarqueurs(nbmq);
			}
			alteration.setSondeFin(tSondeFin.getText());
			alteration.setSondeDebut(tSondeDebut.getText());
			if (tDebut.getValue() != null) {
				alteration.setDebutHg19((Long) tDebut.getValue());
			}
			if (tFin.getValue() != null) {
				alteration.setFinHg19((Long) tFin.getValue());
			}
			alteration.setChromosome(comboChr.getText());
			alteration.setManuel(btManuellement.getSelection());
			alteration.setaVerifBioMol(btnCheckVerifBM.getSelection());
			alteration.setaVerifFish(btnCheckVerifFISH.getSelection());
			alteration.setPrFormule(btnCheckPrFormule.getSelection());

			alteration.setPublier(1);
			alteration.setPrecisionVerif(tGenesInteret.getText());

			if (!tCommentairePA.getText().isEmpty()) {
				if (annotationPA == null) {
					annotationPA = new Annotation();
					annotationPA.setAlteration(alteration);
				}
				ISelection causalPA = comboQualifPA.getSelection();
				if (!causalPA.isEmpty()) {
					IStructuredSelection structuredSelectionca = (IStructuredSelection) causalPA;
					annotationPA.setCausale((TypeCausal) structuredSelectionca.getFirstElement());
				} else {
					TypeCausal tc = new TypeCausal();
					tc.setIdTypeCausal(6);
					annotationPA.setCausale(tc);
				}
				if (comboNotePA.getText() != null && !comboNotePA.getText().isEmpty()) {
					annotationPA.setNote(Integer.decode(comboNotePA.getText()));
				} else {
					annotationPA.setNote(0);
				}
				annotationPA.setNumero(1);

				String auteur = "(" + ApplicationContexte.getInstance().getUtilisateur().getPrenom() + " " + ApplicationContexte.getInstance().getUtilisateur().getNom() + " le " + formatter.format(datedujour) + ")";
				if (!tCommentairePA.getText().equals(annotationPA.getTexte()) && !tCommentairePA.getText().isEmpty()) {
					annotationPA.setTexte(tCommentairePA.getText() + auteur);
				}
			}

			if (!tCommentaireS.getText().isEmpty()) {
				if (annotationS == null) {
					annotationS = new Annotation();
					annotationS.setAlteration(alteration);
				}
				ISelection causalS = comboQualifS.getSelection();
				if (!causalS.isEmpty()) {
					IStructuredSelection structuredSelectionca = (IStructuredSelection) causalS;
					annotationS.setCausale((TypeCausal) structuredSelectionca.getFirstElement());
				} else {
					TypeCausal tc = new TypeCausal();
					tc.setIdTypeCausal(6);
					annotationPA.setCausale(tc);
				}
				if (comboNoteS.getText() != null && !comboNoteS.getText().isEmpty()) {
					annotationS.setNote(Integer.decode(comboNoteS.getText()));
				} else {
					annotationS.setNote(0);
				}
				annotationS.setNumero(2);

				String auteurS = "(" + ApplicationContexte.getInstance().getUtilisateur().getPrenom() + " " + ApplicationContexte.getInstance().getUtilisateur().getNom() + " le " + formatter.format(datedujour) + ")";
				if (!tCommentaireS.getText().equals(annotationS.getTexte()) && !tCommentaireS.getText().isEmpty()) {
					annotationS.setTexte(tCommentaireS.getText() + auteurS);
				}
			}

			alteration.setAnnotationPA(annotationPA);
			alteration.setAnnotationS(annotationS);

			alteration.setAnnotationFinale(comboAnnotationFinale.getText());

		} else {
			return null;
		}
		return alteration;
	}

	public void remplir(final Alteration alt) {
		removeDirtyListener();
		this.alteration = alt;
		if (alteration.getIdAlteration() != 0) {
			TypePuce typepuce = PuceServiceImpl.getInstance().getTypePuceByIdPuce(alt.getPuce().getIdPuce());
			if (alt.getSondeDebut() == null || alt.getSondeDebut().isEmpty()) {
				alt.setSondeDebut(ReferentielServiceImpl.getInstance().getProbeByAlteration(alt.getChromosome(), alt.getDebutHg19(), typepuce.getIdTypePuce()));
			}
			if (alt.getSondeFin() == null || alt.getSondeFin().isEmpty()) {
				alt.setSondeFin(ReferentielServiceImpl.getInstance().getProbeByAlteration(alt.getChromosome(), alt.getFinHg19(), typepuce.getIdTypePuce()));
			}
			cnvTabItem.setText(alteration.getChromosome() + ":" + alteration.getDebutHg19() + "-" + alteration.getFinHg19());

			tConfidence.setValue(alt.getConfidence());

			tNbCopie.setValue(alt.getNbCopie());
			tLog2R.setValue(alt.getLog2R());
			tNbMq.setValue(alt.getNbMarqueurs());
			tTaille.setValue((alt.getFinHg19() - alt.getDebutHg19()) / 1000);
			if (alt.getSondeFin() != null) {
				tSondeFin.setText("" + alt.getSondeFin().trim());
			}
			if (alt.getSondeDebut() != null) {
				tSondeDebut.setText("" + alt.getSondeDebut().trim());
			}
			tFin.setValue(alt.getFinHg19());
			tDebut.setValue(alt.getDebutHg19());
			comboChr.setText("" + alt.getChromosome());
			btManuellement.setSelection(alt.getManuel());
			if (alt.getAnnotationFinale() != null) {
				comboAnnotationFinale.setText("" + alt.getAnnotationFinale());
			} else {
				comboAnnotationFinale.setText("");
			}
			List<GeneNcbi> genes = ReferentielServiceImpl.getInstance().getGeneNCBIByBorne(alt, 0);
			createColumns();
			tableGenes.setInput(genes);
			tNbGenes.setValue(genes.size());

			grpGnesPrsentsDans.pack();

			btnCheckVerifBM.setSelection(alt.isaVerifBioMol());
			btnCheckVerifFISH.setSelection(alt.isaVerifFish());
			btnCheckPrFormule.setSelection(alt.isPrFormule());

			tGenesInteret.setText("" + alt.getPrecisionVerif());
			String txtVerif = "";

			if (alt.isaVerifBioMol()) {
				List<VerifBiomol> verifBiomols = ResultatServiceImpl.getInstance().getVerifBiomolByIdAlteration(alt.getIdAlteration());
				if (verifBiomols != null && verifBiomols.size() > 0) {
					for (VerifBiomol verifBiomol2 : verifBiomols) {

						if (verifBiomol2.getDetecte() != null) {
							if (verifBiomol2.getDetecte() == 1) {
								txtVerif += "CNV détecté par qPCR. ";
								if (verifBiomol2.getHerite() == null) {
									txtVerif += "Origine parentale non vérifiée.";
								} else {
									txtVerif += verifBiomol2.getHerite() + ". ";
								}
							} else if (verifBiomol2.getDetecte() == 0) {
								txtVerif += "CNV non détecté par qPCR. ";
							} else if (verifBiomol2.getDetecte() == 2) {
								txtVerif += "CNV ininterprétable en qPCR. ";
							}
						}
					}
				} else {

					txtVerif += "qPCR demandée";
				}
			}

			if (alt.isaVerifFish()) {
				List<VerifFish> verifFishs = ResultatServiceImpl.getInstance().getVerifFishByIdAlteration(alt.getIdAlteration());
				if (verifFishs != null && verifFishs.size() > 0) {
					for (VerifFish verifFish2 : verifFishs) {
						if (verifFish2.getDetecte() != null && verifFish2.getDetecte()) {
							txtVerif += "CNV détecté par FISH. ";
							if (verifFish2.getHerite() == null) {
								txtVerif += "Origine parentale non vérifiée.";
							} else {
								txtVerif += verifFish2.getHerite() + ". ";
							}
						} else if (verifFish2.getDetecte() == null) {
							txtVerif += "FISH demandée";
						} else if (!verifFish2.getDetecte()) {
							txtVerif += "CNV non détecté par FISH. ";
						}
					}

				} else {
					txtVerif += "FISH demandée";
				}
			}

			tResultatVerif.setText(txtVerif);

			linkNCBI.setText("<a href=\"http://www.ncbi.nlm.nih.gov/projects/mapview/maps.cgi?TAXID=9606&CHR=" + alt.getChromosome() + "&MAPS=ideogr%2Ccntg-r%2Cregions%2CugHs%2Cgenes&BEG=" + alt.getDebutHg19() + "&END=" + alt.getFinHg19() + "&oview=default\">NCBI Map Viewer</a>");
			linkEnsembl.setText("<a  href=\"http://www.ensembl.org/Homo_sapiens/Location/View?r=" + alt.getChromosome() + "%3A" + alt.getDebutHg19() + "-" + alt.getFinHg19() + "\">ENSEMBL</a>");
			linkDGV.setText("<a  href=\"http://dgv.tcag.ca/gb2/gbrowse/dgv2_hg19/?name=chr" + alt.getChromosome() + "%3A" + alt.getDebutHg19() + "-" + alt.getFinHg19() + ";search=Search\">DGV</a>");
			linkDecipher.setText("<a  href=\"https://decipher.sanger.ac.uk/search?q=" + alt.getChromosome() + "%3A" + alt.getDebutHg19() + "-" + alt.getFinHg19() + "\">Decipher</a>");
			linkUCSC.setText("<a  href=\"http://genome.ucsc.edu/cgi-bin/hgTracks?position=chr" + alt.getChromosome() + ":" + alt.getDebutHg19() + "-" + alt.getFinHg19() + "\">UCSC</a>");
			// linkPubMedBande.setText("<a href=\"http://www.ncbi.nlm.nih.gov/pubmed?term=""{$bande}\">PubMed par bande</a>");

			int numCourrant = 0;
			Annotation annotationCourrante = null;
			List<Annotation> annotations = AlterationServiceImpl.getInstance().getAnnotationsByAlteration(alt.getIdAlteration());

			for (Annotation annotation : annotations) {
				if (annotation.getNumero() == 1) {
					annotationPA = annotation;
					alteration.setAnnotationPA(annotationPA);
					comboNotePA.setText("" + annotation.getNote());
					tCommentairePA.setText("" + annotation.getTexte());

					comboQualifPA.setSelection(new StructuredSelection(annotation.getCausale()), true);
					comboNotePA.setText("" + annotation.getNote());
					numCourrant = 1;
				}
				if (annotation.getNumero() > numCourrant) {
					annotationCourrante = annotation;
					numCourrant = annotationCourrante.getNumero();
				}
			}
			if (annotationCourrante != null) {
				comboNoteS.setText("" + annotationCourrante.getNote());
				tCommentaireS.setText("" + annotationCourrante.getTexte());
				comboQualifS.setSelection(new StructuredSelection(annotationCourrante.getCausale()), true);
				comboNoteS.setText("" + annotationCourrante.getNote());
				annotationS = annotationCourrante;
				alteration.setAnnotationS(annotationS);
				numCourrant = 1;
			}
			Utilisateur annotateur = UtilisateurServiceImpl.getInstance().getAnnotateurByIdPuce(alt.getPuce().getIdPuce());
			if (annotateur != null) {
				comboPreAna.setSelection(new StructuredSelection(annotateur), true);
			}

			Utilisateur signataire = UtilisateurServiceImpl.getInstance().getSignataireByIdPuce(alt.getPuce().getIdPuce());
			if (signataire != null) {
				comboSign.setSelection(new StructuredSelection(signataire), true);
			}
			if (graph == null) {
				graph = new GraphCytog(grpGraphique, alt, false);

			}
			if (export != null) {
				export.addSelectionListener(new ExportXLSListener(tableGenes, PuceServiceImpl.getInstance().getNumeroPrelevementByidPuce(alt.getPuce().getIdPuce()) + "_chr" + alt.getChromosome() + "_" + alt.getDebutHg19() + "-" + alt.getFinHg19()));
			}
			;

		} else {
			cnvTabItem.setText("nouveau CNV");
			btManuellement.setSelection(alteration.getManuel());
			btnCheckVerifBM.setSelection(alteration.isaVerifBioMol());
			btnCheckPrFormule.setSelection(alteration.isPrFormule());
			btnCheckVerifFISH.setSelection(alteration.isaVerifFish());

		}
		// final int[] nextId = new int[1];

		composite.pack();
		sc1.redraw();
		setModifiable(isModifiable);
		addDirtyListener();
	}

	public GraphCytog getGraph() {
		return graph;
	}

	public void setGraph(GraphCytog graph) {
		this.graph = graph;
	}

	private void createColumns() {
		if (!colonneExiste) {
			String[] titles = new String[] { "Nom", "Direction", "Type", "Description", "Omim Morbid", "Cancer", "RM/TED", "Omim", "Genecards", "ENSEMBL", "Pubmed", "NCBI Entrez", "DGV", "UCSC" };
			int[] bounds = { 70, 20, 50, 200, 70, 30, 30, 50, 50, 50, 50, 50, 50, 50 };

			TableViewerColumn columnNom = createTableViewerColumn(titles[0], bounds[0], 0);
			TableViewerColumn column2 = createTableViewerColumn(titles[1], bounds[1], 1);
			TableViewerColumn column3 = createTableViewerColumn(titles[2], bounds[2], 2);
			TableViewerColumn column4 = createTableViewerColumn(titles[3], bounds[3], 3);
			TableViewerColumn column5 = createTableViewerColumn(titles[4], bounds[4], 4);
			TableViewerColumn column6 = createTableViewerColumn(titles[5], bounds[5], 5);
			TableViewerColumn column7 = createTableViewerColumn(titles[6], bounds[6], 6);
			TableViewerColumn column8 = createTableViewerColumn(titles[7], bounds[7], 7);
			TableViewerColumn column9 = createTableViewerColumn(titles[8], bounds[8], 8);
			TableViewerColumn column10 = createTableViewerColumn(titles[9], bounds[9], 9);
			TableViewerColumn column11 = createTableViewerColumn(titles[10], bounds[10], 10);
			TableViewerColumn column12 = createTableViewerColumn(titles[11], bounds[11], 11);
			TableViewerColumn column13 = createTableViewerColumn(titles[12], bounds[12], 12);
			TableViewerColumn column14 = createTableViewerColumn(titles[13], bounds[13], 13);
			colonneExiste = true;
		}
	}

	private TableViewerColumn createTableViewerColumn(String header, int width, int idx) {
		TableViewerColumn column = new TableViewerColumn(tableGenes, SWT.LEFT, idx);
		column.getColumn().setText(header);
		column.getColumn().setWidth(width);
		column.getColumn().setResizable(true);
		column.getColumn().setMoveable(true);
		column.setLabelProvider(new GeneTableLabelProvider(idx));
		ColumnViewerToolTipSupport.enableFor(column.getViewer(), ToolTip.NO_RECREATE);
		return column;
	}

	private void removeDirtyListener() {

		tConfidence.getControl().removeModifyListener(modifyListener);
		tLog2R.getControl().removeModifyListener(modifyListener);
		tNbCopie.getControl().removeModifyListener(modifyListener);
		tNbMq.getControl().removeModifyListener(modifyListener);
		tSondeFin.removeModifyListener(modifyListener);
		tSondeDebut.removeModifyListener(modifyListener);
		tFin.getControl().removeModifyListener(modifyListener);
		tDebut.getControl().removeModifyListener(modifyListener);
		comboChr.removeModifyListener(modifyListener);
		tGenesInteret.removeModifyListener(modifyListener);
		tCommentairePA.removeModifyListener(modifyListener);
		tCommentaireS.removeModifyListener(modifyListener);
		comboPreAna.getCombo().removeModifyListener(modifyListener);
		comboSign.getCombo().removeModifyListener(modifyListener);
		comboNoteS.removeModifyListener(modifyListener);
		comboNotePA.removeModifyListener(modifyListener);
		comboQualifS.getCombo().removeModifyListener(modifyListener);
		comboQualifPA.getCombo().removeModifyListener(modifyListener);
		btnCheckVerifBM.removeSelectionListener(selectionListener);
		btnCheckVerifFISH.removeSelectionListener(selectionListener);
		btnCheckPrFormule.removeSelectionListener(selectionListener);
		comboAnnotationFinale.removeModifyListener(modifyListener);

	}

	public void setModifiable(boolean isModifiable) {
		removeDirtyListener();
		this.isModifiable = isModifiable;

		tConfidence.getControl().setEditable(false);
		tNbGenes.getControl().setEditable(false);
		tLog2R.getControl().setEditable(false);
		tNbCopie.getControl().setEditable(false);
		tNbMq.getControl().setEditable(false);
		tTaille.getControl().setEditable(false);
		tSondeFin.setEditable(false);
		tSondeDebut.setEditable(false);
		tFin.getControl().setEditable(false);
		tDebut.getControl().setEditable(false);
		comboChr.setEnabled(false);
		tGenesInteret.setEditable(false);
		tResultatVerif.setEditable(false);
		tCommentairePA.setEditable(false);
		tCommentaireS.setEditable(false);
		btManuellement.setEnabled(false);
		comboPreAna.getCombo().setEnabled(false);
		comboSign.getCombo().setEnabled(false);
		comboNoteS.setEnabled(false);
		comboNotePA.setEnabled(false);
		comboQualifS.getCombo().setEnabled(false);
		comboQualifPA.getCombo().setEnabled(false);
		btnCheckVerifBM.setEnabled(false);
		btnCheckPrFormule.setEnabled(false);
		btnCheckVerifFISH.setEnabled(false);
		comboAnnotationFinale.setEnabled(false);
		if (alteration != null) {
			if (isModifiable) {
				boolean isTermine = ResultatServiceImpl.getInstance().isTermine(alteration.getPuce().getIdPuce());
				if (!isTermine) {
					if (alteration != null && alteration.getManuel()) {
						tConfidence.getControl().setEditable(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "tConfidence"));
						tLog2R.getControl().setEditable(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "tLog2R"));
						tNbCopie.getControl().setEditable(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "tNbCopie"));
						tNbMq.getControl().setEditable(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "tNbMq"));
						tSondeFin.setEditable(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "tSondeFin"));
						tSondeDebut.setEditable(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "tSondeDebut"));
						tFin.getControl().setEditable(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "tFin"));
						tDebut.getControl().setEditable(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "tDebut"));
						comboChr.setEnabled(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "comboChr"));
					}
					if (alteration != null) {
						Puce puce = PuceServiceImpl.getInstance().getPuceByid(alteration.getPuce().getIdPuce());
						comboPreAna.getCombo().setEnabled(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "comboPreAna"));
						comboSign.getCombo().setEnabled(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "comboSign"));
						comboAnnotationFinale.setEnabled(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "comboAnnotationFinale") );

						comboNotePA.setEnabled(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "comboNotePA") && puce.getAnnotateur() != null && puce.getAnnotateur().equals(ApplicationContexte.getInstance().getUtilisateur()));
						comboQualifPA.getCombo().setEnabled(
								DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "comboQualifPA") && puce.getAnnotateur() != null && puce.getAnnotateur().equals(ApplicationContexte.getInstance().getUtilisateur()));
						tCommentairePA.setEditable((DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "tCommentairePA") && puce.getAnnotateur() != null && puce.getAnnotateur().equals(
								ApplicationContexte.getInstance().getUtilisateur())));
						comboNoteS.setEnabled(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "comboNoteS") && puce.getSignataire() != null && puce.getSignataire().equals(ApplicationContexte.getInstance().getUtilisateur()));
						comboQualifS.getCombo().setEnabled(
								DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "comboQualifS") && puce.getSignataire() != null && puce.getSignataire().equals(ApplicationContexte.getInstance().getUtilisateur()));
						tCommentaireS.setEditable(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "tCommentaireS") && puce.getSignataire() != null && puce.getSignataire().equals(ApplicationContexte.getInstance().getUtilisateur()));
						if (btnCheckVerifBM.getSelection()) {
							btnCheckVerifBM.setEnabled(false);
							btnCheckVerifBM.setToolTipText("Pour des raisons de traçabilité, une fois selectionnée, il est impossible d'annuler une vérification, il faut aller sur l'analyse qPCR créée pour indiquer dans le résultat que celle ci a été annulée");
						} else {
							btnCheckVerifBM.setEnabled(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "btnCheckVerifBM"));
						}
						if (btnCheckVerifFISH.getSelection()) {
							btnCheckVerifFISH.setEnabled(false);
							btnCheckVerifFISH.setToolTipText("Pour des raisons de traçabilité, une fois selectionnée, il est impossible d'annuler une vérification, il faut aller sur l'analyse FISH créée pour indiquer dans le résultat que celle ci a été annulée");
						} else {
							btnCheckVerifFISH.setEnabled(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "btnCheckVerifFISH"));
						}
						btnCheckPrFormule.setEnabled(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "btnCheckPrFormule"));

						tGenesInteret.setEditable(DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "tGenesInteret"));
					}
				} else {
					MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Information", "La puce est déjà rendue ou en échec, il est impossible d'ajouter ou de modifier une annotation");
				}
				addDirtyListener();
			}
		}

	}

	public boolean verifierChamp() {
		if (comboChr.getText() == null || comboChr.getText().isEmpty() && DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "comboChr")) {
			MessageDialog.openError(Display.getCurrent().getActiveShell(), "Champs obligatoire manquant", "Onglet CNV : Champs obligatoire manquant : Chromosome");
			return false;
		}
		if (tNbMq.getValue() == null || tNbMq.getValue().equals("") && DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "tNbMq")) {
			MessageDialog.openError(Display.getCurrent().getActiveShell(), "Champs obligatoire manquant", "Onglet CNV : Champs obligatoire manquant : Nombre de marqueurs");
			return false;
		}
		if (tDebut.getValue() == null || tDebut.getValue().equals("") && DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "tDebut")) {
			MessageDialog.openError(Display.getCurrent().getActiveShell(), "Champs obligatoire manquant", "Onglet CNV : Champs obligatoire manquant : Début");
			return false;
		} else {
			if ((Long) tDebut.getValue() <= 0) {
				MessageDialog.openError(Display.getCurrent().getActiveShell(), "Erreur de format", "Onglet CNV : Début doit être un entier positif");
				return false;
			}

		}
		if (tFin.getValue() == null || tFin.getValue().equals("") && DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "tFin")) {
			MessageDialog.openError(Display.getCurrent().getActiveShell(), "Champs obligatoire manquant", "Onglet CNV : Champs obligatoire manquant : Fin");
			return false;
		} else {
			if ((Long) tFin.getValue() <= 0) {
				MessageDialog.openError(Display.getCurrent().getActiveShell(), "Erreur de format", "Onglet CNV : Fin doit être un entier positif");
				return false;
			}
		}
		if ((tLog2R.getValue() == null || tLog2R.getValue().equals("")) && (tNbCopie.getValue() == null || tNbCopie.getValue().equals("")) && DroitServiceImpl.getInstance().peutModifierByUtilisateurAndChamp(ApplicationContexte.getInstance().getUtilisateur(), "tLog2R")) {
			MessageDialog.openError(Display.getCurrent().getActiveShell(), "Champs obligatoire manquant", "Onglet CNV : Champs obligatoire manquant : Lod2R ou Nombre de copies");
			return false;
		}
		if ((Long) tDebut.getValue() >= (Long) tFin.getValue()) {
			MessageDialog.openError(Display.getCurrent().getActiveShell(), "Erreur", "Onglet CNV : le début doit obligatoirement être inférieur à la fin.");
			return false;
		}
		return true;
	}

	public Alteration getCNV() {

		return alteration;
	}

}
